package com.mdjnu.service.imp;

import com.mdjnu.mapper.CommentMapper;
import com.mdjnu.pojo.Comment;
import com.mdjnu.pojo.Result;
import com.mdjnu.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
@Service// 注入mapper
public class CommentServiceImp implements CommentService {
    //注入mapper
    @Autowired
    private CommentMapper  commentMapper;
    //添加评论
    @Override

    public Result addComment(Comment comment) {
        //  设置默认值
        comment.setLikeCount(0);// 设置点赞数
        comment.setDislikeCount(0);//  设置踩数
        comment.setCommentTime(LocalDateTime.now());//  设置时间
        int i = commentMapper.addComment(comment);//  添加
        if(i>0){// 添加成功
            return Result.success();
        }else{//  添加失败
            return Result.error("评论失败");

        }
    }

    @Override//
    public Result findByVideoId(int videoId, int pageNum, int pageSize) {// 根据视频id查询评论
        int start = (pageNum-1)*pageSize;// 计算起始位置
        return Result.success(commentMapper.findByVideoId(videoId,start,pageSize));// 返回结果
    }
}
